import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class Theater extends JFrame implements ActionListener  
{
	ArrayList<Movie> playList =new ArrayList<Movie>();
	String[] movielist= {"Titanic","The Good, the Bad and the Ugly","The Godfather","The Dark Knight","The Passion of the Christ","Shrek","Harry Potter","Avatar","Die Hard","Saving Private Ryan"};
	
	JLabel title;
	JLabel [] schedule=new JLabel[10];
	JButton openTheater;
	JComboBox choices;
	JTextArea txtbox;
	int customerIdGenerator=1;
	
	Semaphore fullTheatre=new Semaphore(10);
	Semaphore mutex =new Semaphore(1);
	Semaphore consectutiveMovies=new Semaphore(0);
	Semaphore mutex1=new Semaphore(1);

	
	
	int Consectutive=0;
	Thread createCustomersthread;

	public Theater()
	{
		super();
		setBounds(200,50,1000, 600);
		setTitle("The Movie Theater");
		setLayout(null);
    	getContentPane().setBackground(Color.BLACK);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	
    	title =new JLabel("The Movie Theater");
    	title.setFont(new Font("Calibri", Font.PLAIN, 34));
    	title.setForeground(Color.white);
    	title.setBounds(350, 5, 400, 100);
    	getContentPane().add(title);   
    	
    	
//    	choices = new JComboBox( dropdownlist ); 
//    	choices.setBounds(350,120,200,30); 
//    	add(choices);
    	
    	openTheater=new JButton("Open Theater");
    	openTheater.addActionListener(this);
    	openTheater.setFont(new Font("Myriad Pro",Font.BOLD,14));
    	openTheater.setBounds(430,120, 200, 30);
    	openTheater.setBackground(new Color(177,32,35));
    	openTheater.setForeground(Color.WHITE);
    	getContentPane().add(openTheater);   
    	
 
		 	JLabel sch =new JLabel("Today's Schedule");
		 	sch.setFont(new Font("Calibri", Font.ITALIC, 20));
		 	sch.setForeground(Color.LIGHT_GRAY);
		 	sch.setBounds(50, 80, 400, 100);
	    	getContentPane().add(sch);   
		 
	    	for(int i=0;i<10;i++)
				schedule[i]=new JLabel();
	    	
	    	txtbox =new JTextArea();
	    	txtbox.setFont(new Font("Berlin Sans FB Demi", Font.PLAIN, 15));
	    	JScrollPane scrollPane = new JScrollPane(txtbox);
	    	scrollPane.setBounds(430, 180, 480, 325);
	    	add(scrollPane);
	    	 
	    	
	    	
		 
			
	}

 
	 //Checks if the movie already exists in the playlist, if it does, it returns its index
	public int getIndex(String moviename)  
	{

		for(int i=playList.size()-1;i>=0;i--)
		{
	//		System.out.println(playList.get(i).moviename+" the second movie"+ movienam);
			if(playList.get(i).moviename.equals(moviename))
				return i;
		}
		return -1;
	}
	 
	public boolean  addMovie( Movie m )
	{
		 
		if( playList.size() < 10 )
		{
			 
			playList.add( m );
				
			
			updateSchedule();
			
			if(Consectutive>0)
				UpsSemaphore(consectutiveMovies,Consectutive);
			
			
			return true;
		}
		else return false;		
		 
	}
	
	public boolean isLatestMovie(int movieindex)
	{
		if(playList.size()-1==movieindex)
			return true;
		return false;
	}
	
	public void updateSchedule()
	{
		 
		
		for(int i=0;i<10;i++)
			schedule[i].setText("");
		
		int x=110;
		 for(int i=0;i<playList.size();i++)
		 {	 
			 if(playList.get(i).isWatched)
			 {
				schedule[i].setText((i+1)+") "+playList.get(i).moviename+ " "+playList.get(i).customerList.size()+" - Watched") ;
				schedule[i].setForeground(Color.GREEN); 
			 }
			 else
			 {
				schedule[i].setText((i+1)+") "+playList.get(i).moviename+ " "+playList.get(i).customerList.size()) ;
				schedule[i].setForeground(Color.white);
			 }
			 			 
				schedule[i].setFont(new Font("Calibri", Font.PLAIN, 16));
				schedule[i].setBounds(60, x, 400, 100);
		    	getContentPane().add(schedule[i]);   
		    	x+=20;
		    	
		 }
		 repaint();
	}
	
	public void Printer(String s)
	{
		String x= s+"\n";
		txtbox.append(x);
	}
	

	public void UpsSemaphore(Semaphore S,int upNo)
	{
		for(int i=0;i<Consectutive;i++)
		{
			S.up();
		}
			
	}
	
	public void actionPerformed(ActionEvent event) 
	{
		if ( event.getSource() == openTheater)
		{
			int number;
			
	    	for(int i=0;i<80;i++)
	    	{
	    		number= (int)(Math.random()*10); 
	    		new Customer(this,"Customer "+customerIdGenerator,movielist[number]);
	    		customerIdGenerator++;
			 
	    	} 
	    	 
	
		}
		
	}
	
	public Movie getFirstMovie()
	{
		for(int i=0;i<playList.size();i++)
		{
			if(playList.get(i).isWatched==false)
				return playList.get(i);
		}
		return null;
	}
	
	public static void main(String [] args)
	{
		Theater theater =new Theater();
		theater.setVisible(true);
	}


	 
	
}
